/**
 * 
 */
package com.wubo.sec.web.action;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.bean.MenuAuthRelate;
import com.wubo.sec.service.IMenuAuthService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-9
 * @version 1.0.01
 */
@Controller
public class MenuAuthMgtAction extends BaseSecAction {
	@Autowired
	private IMenuAuthService menuAuthService;
	
	private String menuCode;
	
	private String authCode;


	public IMenuAuthService getMenuAuthService() {
		return menuAuthService;
	}

	public void setMenuAuthService(IMenuAuthService menuAuthService) {
		this.menuAuthService = menuAuthService;
	}

	public String getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
	
	public String show(){
		try{
			authCode = menuAuthService.getAuthCode(menuCode);
			
			logger.debug("Find menu's auth .MenuCode:"+menuCode+", AuthCode:"+authCode);
			
			jsonSuccess(new JSONSerializer().serialize(authCode), true);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String relate(){
		logger.debug("relate auths to Menu .MenuCode:"+menuCode+", AuthCode:"+authCode);

		try{
			if(! StringUtils.hasText(authCode)){
				authCode = null;
			}
			menuAuthService.relate( new MenuAuthRelate(menuCode, authCode) );
			
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
}
